//
//  NavRootShowViewContorller.m
//  YZZX
//
//  Created by Easaa on 15/4/29.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "NavRootShowViewContorller.h"

@interface NavRootShowViewContorller ()<HXHttpRequestDelegate,UIScrollViewDelegate>
{
    HXHttpRequest      *_httpRequest;
    NSMutableArray     *DataModel;
}

@end

@implementation NavRootShowViewContorller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"栏目";
    DataModel = [[NSMutableArray alloc]init];
    
    //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],    @"user_id",
                               @"1",    @"site_id",
                               nil];
    
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_CATEGORY_NAVIGATION_LIST andTag:1001];//@"system.time"];//@"category.list"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}

-(void)CateButtonList
{
    
    UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, Dev_ScreenWidth, Dev_ScreenHeight-64)];
    scrollview.delegate = self;
    scrollview.contentSize = CGSizeMake(Dev_ScreenWidth, (DataModel.count/4+2)*Dev_ScreenWidth/4);
    scrollview.bounces = NO;
    [scrollview setPagingEnabled:YES];
    [scrollview setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:scrollview];

   
    for (int i = 0; i<DataModel.count; i++)
    {
        CategoryDataModel *cellmodel = [DataModel objectAtIndex:i];
        UIControl *Control =[[UIControl alloc]initWithFrame:CGRectMake(Dev_ScreenWidth/4*(i%4),Dev_ScreenWidth/4*(i/4),Dev_ScreenWidth/4,Dev_ScreenWidth/4)];
        Control.tag =i;
        [Control addTarget:self action:@selector(navbutfantion:) forControlEvents:UIControlEventTouchUpInside];
        [scrollview addSubview:Control];
        
        [Control.layer setMasksToBounds:YES];
        [Control.layer setCornerRadius:1.0]; //设置矩圆角半径
        [Control.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.1 });
        [Control.layer setBorderColor:colorref];//边框颜色

        
        
        UIImageView *imagviewconten =[[UIImageView alloc]initWithFrame:CGRectMake((Dev_ScreenWidth/4-40)/2, 10, 40, 40)];
        //imagviewconten.isUserInteractionEnabled =NO;
        [Control addSubview:imagviewconten];
        [imagviewconten sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.hellomommy.cn%@",cellmodel.image_url]] placeholderImage:[UIImage imageNamed:@"bg"]];
         [imagviewconten sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.hellomommy.cn%@",cellmodel.image_url]]];
        
        
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, Dev_ScreenWidth/4, 20)];
        labelTitle.font = [UIFont boldSystemFontOfSize:16.0];  //UILabel的字体大小
        labelTitle.numberOfLines = 1;  //必须定义这个属性，否则UILabel不会换行
//        labelTitle.textColor = RGBCOLOR(0, 0, 147);
        labelTitle.textAlignment = NSTextAlignmentCenter;  //文本对齐方式
        //[labelTitle setBackgroundColor:[UIColor redColor]];
        labelTitle.text =cellmodel.cate_name;
        [Control addSubview:labelTitle];
    }


}
-(void)navbutfantion:(UIControl *)ControlFantion
{
    CategoryDataModel *ChangView =  DataModel[ControlFantion.tag];
    //添加 字典，将label的值通过key值设置传递
    NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:ChangView.cate_id,@"cate_id",nil];
    

    //创建通知
    NSNotification *notification =[NSNotification notificationWithName:YZZX_LANMU_CHANG object:nil userInfo:dict];
    //通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    //tableview列表数据
    if(tag == 1001)
    {
        //  NSLog(@"---------------->>>> %@",responseString);
        
        NSDictionary *dic = [[NSDictionary alloc]init];
        dic = [JsonUtil jsonToDic:responseString];
        
        // NSLog(@"---------------->>>> \n%@",dic);
        
        NSString * strs = dic[@"data"];
        NSDictionary *dics =[JsonUtil jsonToDic:strs];
        
        
        NSArray *arryy =(NSArray *)dics;
        
        
        
        NSLog(@"---------00------->>>> %@",arryy);
        
        for (NSDictionary *dicuu in arryy)
        {
            
            // NSLog(@"------->>>> %@",[dicuu objectForKey:@"Image"]);
            
            CategoryDataModel *cellmodel            =[[CategoryDataModel alloc]init];
            cellmodel.cate_name                     = [dicuu objectForKey:@"cate_name"];
            cellmodel.cate_id                       = [[dicuu objectForKey:@"cate_id"] stringValue];
            cellmodel.page_type                     = [dicuu objectForKey:@"page_type"];
            cellmodel.image_url                     = [dicuu objectForKey:@"image_url"];
            cellmodel.config                        = [dicuu objectForKey:@"config"];
            cellmodel.cate_type                     = [dicuu objectForKey:@"cate_type"];
            [DataModel addObject:cellmodel];
            

        }
        [self CateButtonList];
    }
    
}

- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
    
    
    

}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"123" object:Nil];
}



@end
